/**
 * 公共数据请求方法
 * @param {object} req 请求参数
 */

// export const sysurl = 'https://gain.shunchenghc.com/'; //生产 
// export const sysurl = 'https://app.shunchenghc.com/'; //测试 
export const sysurl = 'https://lskj.lskejisoft.com/shujun/bizunion-server/'; //测试 

const request = (req) => {
	// 	//收起虚拟键盘
	uni.hideKeyboard();
	// 	// //检查网络是否正常
	var isNet = true;
	uni.onNetworkStatusChange(function(res) {
		isNet = res.isConnected;
	});
	if (!isNet) return uni.showToast({
		title: '网络无法连接,请检测您的网络是否正常',
		icon: 'none',
		position: 'bottom'
	});
	// 	//配置请求参数

	var dstURL = sysurl + req.adress;
	return new Promise((resolve, reject) => {
		uni.request({
			url: dstURL,
			method: req.mod || "GET", //POST,
			data: req.data || {},
			header: req.head || {
				"Accept": "*/*",
				'Content-Type': 'application/x-www-form-urlencoded',
				"Authorization": uni.getStorageSync('token'),
				'dmid': "INDUSTRY_CATEGORY"
			},
			timeout: req.times || 60000,
			success: (res) => {
				if (res.statusCode === 200 || res.statusCode === 203) {
					resolve(res.data);
				} else {
					handleError(res);
					reject(res);
				}
			},
			fail: (error) => {
				uni.showToast({
					title: '网络错误',
					icon: 'none'
				});
				reject(error);
			},
		});
	});
};

const handleError = (response) => {
	switch (response.statusCode) {
		case 401:
			uni.removeStorageSync('token');
			uni.showToast({
				title: '请先登录',
				icon: 'none'
			});
			// 跳转到登录页的逻辑
			break;
		case 404:
			uni.showToast({
				title: '资源未找到',
				icon: 'none'
			});
			break;
		default:
			uni.showToast({
				title: '请求错误',
				icon: 'none'
			});
			break;
	}
};



export default request